package al.xc.task;

import al.xc.common.util.TimeUtils;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class DelayInfo implements Delayed {

    private final long delay;

    DelayInfo(Long millisecond) {
        this.delay = TimeUtils.getTotalMillis() + millisecond;
    }

    @Override
    public long getDelay(TimeUnit timeUnit) {
        return timeUnit.convert(delay - TimeUtils.getTotalMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed delayed) {
        if (delayed == this) return 0;

        if (delayed instanceof DelayInfo) {
            DelayInfo info = (DelayInfo)delayed;
            return (int)(this.delay - info.delay);
        }
        return 0;
    }
}
